﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMM.Objects
{
    /// <summary>
    /// Object to represent a found media file during file-scan and import.
    /// </summary>
    public class MediaInfo : Object
    {
        /// <summary>
        /// Full Path to primary file.
        /// </summary>
        /// <remarks>all secondary files are listed within the UserdefinedMediaInfo-Object.</remarks>
        public string FullPath { get; set; }

        /// <summary>
        /// Name of the primary file.
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// Path of the primary file.
        /// </summary>
        public string DirectoryName { get; set; }

        /// <summary>
        /// Internal Media-Type.
        /// </summary>
        /// <remarks>0...Movie
        /// 1...SeriesEpisode
        /// 2...Music
        /// 3...Picture</remarks>
        public int MediaType { get; set; }

        /// <summary>
        /// Check if File is present in database.
        /// </summary>
        public bool NewFile
        {
            get
            {
                return checkNewFile();
            }
        }

        /// <summary>
        /// Marker for MediaInfo-Items that have user-data.
        /// </summary>
        public bool IsEdited { get; set; }

        /// <summary>
        /// Holding additional data.
        /// </summary>
        /// <remarks>Can be Movie(), SeriesEpisode(), AudioTrack(), Picture().</remarks>
        public object UserdefinedMediaInfo { get; set; }

        /// <summary>
        /// Full path to selected CoverArt-Image.
        /// </summary>
        public string CoverImagePath { get; set; }

        /// <summary>
        /// Audio-Info of the primary-file.
        /// </summary>
        public Objects.AudioInfo File0AudioInfo { get; set; }

        /// <summary>
        /// Video-Info of the primary-file.
        /// </summary>
        public Objects.VideoInfo File0VideoInfo { get; set; }

        /// <summary>
        /// Status for ListView() and import-process.
        /// </summary>
        public string Status
        { get { return getStatus(); } }

        private string getImportInfoString()
        {
            string sInfoString = String.Empty;

            if (UserdefinedMediaInfo != null)
            {
                if (MediaType == 0 || MediaType == 1) // Movie
                {
                    Objects.Movie oMovie = (Objects.Movie)UserdefinedMediaInfo;
                    sInfoString = oMovie.Name + " (" + oMovie.Year + ")";
                }
                else if (MediaType == 2) // Episode
                {
                    Objects.SeriesEpisode oEpisode = (Objects.SeriesEpisode)UserdefinedMediaInfo;
                    Objects.Series oShow = new Series(oEpisode.SeriesID);
                    sInfoString = oShow.Name;

                    if (oEpisode.SeasonID != 0)
                    {
                        Objects.SeriesSeason oSeason = new SeriesSeason(oEpisode.SeasonID);
                        sInfoString += " (S" + oSeason.Number.ToString();

                        if (oEpisode.EpisodeNo == 0)
                            sInfoString += ")";
                        else
                            sInfoString += " ";
                    }

                    if (oEpisode.EpisodeNo != 0)
                    {
                        if (oEpisode.SeasonID == 0)
                            sInfoString += " (";

                        sInfoString += "E" + oEpisode.EpisodeNo.ToString() + ")";
                    }
                }
            }
            else
            {
                return FileName;
            }

            return sInfoString;
        }

        public string ImportInfoString
        { get { return getImportInfoString(); } }

        /// <summary>
        /// Get Status for ListView() and import-process.
        /// </summary>
        /// <returns>(string) Status-String.</returns>
        private string getStatus()
        {
            if (NewFile == false)
                return "OK";
            else
            {
                if (IsEdited)
                    return "!";
                else
                    return "*";
            }
        }

        public bool checkNewFile()
        {
            if (ItemChecks.checkFileExists(FullPath))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
